package com.egao.common.publicly;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cooperative.constants.CooperativeProjectConstants;
import com.egao.common.module.cooperative.entity.CooperativeProject;
import com.egao.common.module.cooperative.entity.CooperativeProjectActivity;
import com.egao.common.module.cooperative.entity.CooperativeProjectActivityStudent;
import com.egao.common.module.cooperative.entity.vo.CooperativeProjectActivityListVO;
import com.egao.common.module.cooperative.entity.vo.CooperativeProjectListVO;
import com.egao.common.module.cooperative.service.CooperativeProjectActivityService;
import com.egao.common.module.cooperative.service.CooperativeProjectActivityStudentService;
import com.egao.common.module.cooperative.service.CooperativeProjectBannerService;
import com.egao.common.module.cooperative.service.CooperativeProjectService;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.publicly.service.PubliclyCooperativeProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 产教融合公开接口
 * @author root
 */
@Api(tags = "产教融合")
@RestController
@RequestMapping("${api}")
public class PubliclyCooperativeController extends BaseController {

    @Autowired
    private CooperativeProjectBannerService cooperativeProjectBannerService;
    @Autowired
    private CooperativeProjectActivityService cooperativeProjectActivityService;
    @Autowired
    private CooperativeProjectService cooperativeProjectService;
    @Autowired
    private CooperativeProjectActivityStudentService cooperativeProjectActivityStudentService;
    @Autowired
    private StudentService studentService;

    @ApiOperation("banner")
    @GetMapping("/publicly/cooperative/banner")
    public JsonResult list(){
        return JsonResult.ok().setData(cooperativeProjectBannerService.listPublicity());
    }

    @ApiOperation("项目列表")
    @GetMapping("/publicly/cooperative/project")
    public PageResult<CooperativeProjectListVO> projectList(HttpServletRequest request) {
        PageParam<CooperativeProject> pageParam = new PageParam<>(request);
        List<CooperativeProjectListVO> list = cooperativeProjectService.listPagePublicity(pageParam);
        return new PageResult<>(list, pageParam.getTotal());
    }

    @ApiOperation("项目详情")
    @GetMapping("/publicly/cooperative/project-detail")
    public JsonResult projectDetail(Integer id) {
        CooperativeProject project = cooperativeProjectService.getById(id);
        if (project == null) {
            return JsonResult.error("信息不存在");
        }

        // 累计点击量
        cooperativeProjectService.addHits(project);

        // 查询活动、风采、动态
        PageParam<CooperativeProjectActivity> pageParam = new PageParam<>();
        pageParam.setCurrent(1);
        pageParam.setSize(999);
        List<CooperativeProjectActivityListVO> list = cooperativeProjectActivityService.listPagePublicity(pageParam);

        List<CooperativeProjectActivityListVO> activityList = new ArrayList<>();
        List<CooperativeProjectActivityListVO> shineList = new ArrayList<>();
        List<CooperativeProjectActivityListVO> dynamicList = new ArrayList<>();

        // 封装不同的信息到不同的容器
        for (CooperativeProjectActivityListVO item : list) {
            switch (item.getType()) {
                case CooperativeProjectConstants.CPA_TYPE_ACTIVITY:
                    activityList.add(item);
                    break;
                case CooperativeProjectConstants.CPA_TYPE_SHINE:
                    shineList.add(item);
                    break;
                case CooperativeProjectConstants.CPA_TYPE_DYNAMIC:
                    dynamicList.add(item);
                    break;
                default:
            }
        }

        Map<String, List<CooperativeProjectActivityListVO>> map = new HashMap<>();
        map.put("activityList", activityList);
        map.put("shineList", shineList);
        map.put("dynamicList", dynamicList);

        return JsonResult.ok().setData(project).put("list", map);
    }

    @ApiOperation("活动列表")
    @GetMapping("/publicly/cooperative/activity")
    public PageResult<CooperativeProjectActivityListVO> activityList(HttpServletRequest request) {
        PageParam<CooperativeProjectActivity> pageParam = new PageParam<>(request);
        List<CooperativeProjectActivityListVO> list = cooperativeProjectActivityService.listPagePublicity(pageParam);
        return new PageResult<>(list, pageParam.getTotal());
    }

    @ApiOperation("活动详情")
    @GetMapping("/publicly/cooperative/activity-detail")
    public JsonResult activityDetail(Integer id) {
        CooperativeProjectActivity activity = cooperativeProjectActivityService.getById(id);
        if (activity == null) {
            return JsonResult.error("信息不存在");
        }
        return JsonResult.ok().setData(activity).put("btn", cooperativeProjectActivityService.initBtn(activity, getLoginUser()));
    }

    @OperLog(value = "产教融合", desc = "学生报名", result = true)
    @ApiOperation(value = "产教融合学生报名")
    @PostMapping("/publicly/cooperative/apply")
    public JsonResult apply(@RequestBody CooperativeProjectActivityStudent cooperativeProjectActivityStudent) {
        Integer userId = getLoginUserId();
        if (userId == null) {
            return JsonResult.error("请先登录");
        }

        Student student = studentService.getByUserId(userId);
        if (student == null) {
            return JsonResult.error("学生信息异常");
        }

        cooperativeProjectActivityStudent.setStudentId(student.getId());
        return cooperativeProjectActivityStudentService.apply(cooperativeProjectActivityStudent);
    }

    @OperLog(value = "产教融合", desc = "学生取消报名", result = true)
    @ApiOperation(value = "产教融合学生取消报名")
    @PutMapping("/publicly/cooperative/cancel")
    public JsonResult cancel(@RequestBody CooperativeProjectActivityStudent cooperativeProjectActivityStudent) {
        return cooperativeProjectActivityStudentService.cancel(cooperativeProjectActivityStudent);
    }
}
